<template>
  <div class="xtx-pagination">
    <a href="javascript:;" class="disabled" v-if="currPage <= 1">上一页</a>
    <a href="javascript:;" v-else @click="changePage(currPage - 1)">上一页</a>
    <span v-if="pager.start > 1">...</span>
    <a
      href="javascript:;"
      :class="{ active: i === currPage }"
      v-for="i in pager.btnArr"
      :key="i"
      @click="changePage(i)"
      >{{ i }}</a
    >
    <span v-if="pager.end < pager.pageCount">...</span>
    <a href="javascript:;" class="disabled" v-if="currPage >= pager.pageCount"
      >下一页</a
    >
    <a href="javascript:;" v-else @click="changePage(currPage + 1)">下一页</a>
  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    total: {
      type: Number,
      default: 100
    },
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    }
  },
  setup(props, { emit }) {
    // 总条数，测试数据
    const total = ref(101)
    // 每页条数
    const pageSize = ref(10)
    // 当前第几页
    const currPage = ref(1)
    // 按钮个数
    const btnCount = ref(5)

    // 💥重点：根据上述数据得到（总页数，起始页码，结束页码，按钮数组）
    const pager = computed(() => {
      // 计算总页数
      const pageCount = Math.ceil(total.value / pageSize.value)
      // 计算启始页吗和结束页吗
      // 1.理想情况根据当前页码，和按钮个数得到
      let start = currPage.value - Math.floor(btnCount.value / 2)
      // 2.1 如果启始页码小于1了，需要重新计算
      if (start <= 1) start = 1

      let end = start + btnCount.value - 1
      // 2.2 如果结束页码大于总页数，需要重新计算
      if (end > pageCount) {
        end = pageCount
        start = end - btnCount.value + 1
        if (start <= 1) start = 1
      }
      // 处理完毕start和end得到按钮数组
      const btnArr = []
      for (let i = start; i <= end; i++) {
        btnArr.push(i)
      }
      return { pageCount, start, end, btnArr }
    })
    // 监听传入的值改变
    watch(
      props,
      () => {
        total.value = props.total
        currPage.value = props.currentPage
        pageSize.value = props.pageSize
      },
      { immediate: true }
    )
    // 切换改变页码
    const changePage = (page) => {
      if (page < 1 || page > pager.value.pageCount || currPage.value === page) { return }
      currPage.value = page
      // 通知父组件最新页码
      emit('current-change', page)
      // v-model:current-page 当前页码，支持双向绑定更新
      emit('update:current-page', page)
    }
    return { currPage, pager, changePage }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @color;
    }
    &.active {
      background-color: @color;
      color: #fff;
      border-color: @color;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>
